﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace SharePointSocialNetworking.Webparts.FacebookLikeButtonWebPart
{
    [ToolboxItemAttribute(false)]
    public class FacebookLikeButtonWebPart : WebPart
    {
        #region properties
        private string layout = "standard";
        public string Layout
        {
            get { return layout; }
            set { layout = value; }
        }
        public bool Faces { get; set; }
        private string facebookWidth = "450";
        public string FacebookWidth
        {
            get { return facebookWidth; }
            set { facebookWidth = value; }
        }
        private string verb = "like";
        public string Verb
        {
            get { return verb; }
            set { verb = value; }
        }
        private string facebookfont = "arial";
        public string FacebookFont
        {
            get { return facebookfont; }
            set { facebookfont = value; }
        }
        private string scheme = "light";
        public string Scheme
        {
            get { return scheme; }
            set { scheme = value; }
        }
        private string facebookHeight = "35";
        public string FacebookHeight
        {
            get { return facebookHeight; }
            set { facebookHeight = value; }
        }
        #endregion

        #region constructor
        public FacebookLikeButtonWebPart()
        {
            this.ChromeType = PartChromeType.None;
            this.Title = "Facebook Like Button Web Part";
        }
        #endregion

        #region overrides
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e); 
            ClientScriptManager csm = Page.ClientScript;
            if (!csm.IsClientScriptIncludeRegistered("jQuery"))
                csm.RegisterClientScriptInclude(this.GetType(), "jQuery", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"); // register jquery.
            if(!csm.IsClientScriptIncludeRegistered("jQueryTools"))
                csm.RegisterClientScriptInclude(this.GetType(), "jQueryTools", "http://cdn.jquerytools.org/1.2.5/tiny/jquery.tools.min.js"); // register jquery tools.
            if(!csm.IsClientScriptIncludeRegistered("facebookQuestions"))
                csm.RegisterClientScriptInclude(this.GetType(), "facebookQuestions", string.Format("{0}/_catalogs/SocialSharePoint/Javascripts/FacebookLikeButtonHandler.js", SPContext.Current.Web.Url)); // register facebook questions script.
            
            CssLink css = new CssLink();
            css.DefaultUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/CSS/FacebookLikeButtonStyle.css";
            this.Page.Header.Controls.Add(css);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // renders the facebook script with the variables we have chosen in the editor part.
            writer.Write("<iframe src=\"http://www.facebook.com/plugins/like.php?href={0}&amp;layout={1}&amp;show_faces={2}&amp;width={3}&amp;action={4}&amp;font={5}&amp;colorscheme={6}&amp;height={7}\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:{3}px; height:{7}px;\" allowTransparency=\"true\"></iframe>",
                SPEncode.UrlEncode(HttpContext.Current.Request.Url.ToString()), Layout, Faces, FacebookWidth, Verb, FacebookFont, Scheme, FacebookHeight);
        }

        public override EditorPartCollection CreateEditorParts()
        {
            // adds the editor part to the web part.
            return new EditorPartCollection(
                new EditorPart[] { new FacebookLikeButtonEditorPart() });
        }
        #endregion
    }
}
